package com.bdqn.ch05jsp;

import com.bdqn.ch05jsp.filter.FilterConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Ch05WebMvcTester {

    @Resource
    private WebApplicationContext wac;//注入 Spring Web 上下文的环境

    private MockMvc mockMvc;

    @Resource
    ApplicationContext applicationContext;

    /**
     * 执行测试方法之前的操作
     */
    @Before
    public void setup() {  //初始化spring web的上下文环境
        System.out.println("初始化spring web的上下文环境");
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    //测试http请求  http://localhost:8080/hello/100
    @Test
    public void testHttpHelloTester() throws Exception {
        String mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/hello/200"))//测试地址
                .andReturn().getResponse().getContentAsString();//返回响应主体内容
        System.out.println("Result === " + mvcResult);
    }


    /**
     * 查看spring容器中的bean
     */
    @Test
    public void testHelloTester() {
        /*   ApplicationContext applicationContext = new  ClassPathXmlApplicationContext("applicationContext.xml");*/
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }

        FilterConfig filterConfig = (FilterConfig) applicationContext.getBean("filterConfig");
        System.out.println(filterConfig);
    }
}
